<template>
  <div class="container">
    <div class="marks">
      <img src="../../images/tradeicon.png" alt="" />
      <img src="../../images/trademark-active.png" alt="" />
    </div>
    <div class="rCard">
      <div class="head">
        <div class="headline">
          <span>创建您的帐户</span>
        </div>
        <div class="intro">
          <span
            >单击“注册”即表示您同意我们的服务条款并确认您已阅读我们的隐私政策。</span
          >
        </div>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="rForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >注册</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        pass: "",
        checkPass: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="scss">
.container {
  .marks {
    position: absolute;
    top: 20vh;
    left: 25vw;
    img:nth-child(2) {
      width: 10vw;
      height: 8vh;
      position: relative;
      bottom: 2vh;
    }
    img:nth-child(1) {
      width: 5vw;
      height: 10vh;
      position: relative;
      left: 1vw;
    }
  }
  .rCard {
    border: 1px solid #dcdfe6;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    position: absolute;
    top: 15vh;
    left: 60vw;
    width: 25vw;
    height: 60vh;

    .head {
      margin: 10% 10% 0 10%;
      .headline {
        font-size: 1.25rem;
        letter-spacing: 0.15rem;
        font-weight: bold;
      }
      .intro {
        font-size: 12px;
        padding: 10px 50px 0px 0px;
        line-height: 20px;
      }
    }
    .rForm {
      width: 80%;
      margin: 0 10%;
      .el-form-item {
        margin-bottom: 1vh;
      }
      .el-form-item__label {
        height: 3.5vh;
        color: black;
      }
      .el-form-item__content {
        .el-form-item__error {
          padding: 0;
        }
        .el-input {
          height: 4vh;
          width: 100%;
          .el-input__inner {
            height: 80%;
          }
        }
      }
      .btn {
        margin-top: 5vh;
        display: flex;
        flex-direction: column;
        .el-button--primary {
          width: 100%;
          background-color: #1b75d0;
        }
        .el-button--default {
          width: 100%;
          margin: 2.5vh 0px 0px 0px;
        }
      }
    }
  }
}
</style>
